package cn.wzptsoft.mall.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.wzptsoft.mall.aspect.AutoFull;
import cn.wzptsoft.mall.aspect.OperationType;
import cn.wzptsoft.mall.mapper.PosterItemMapper;
import cn.wzptsoft.mall.mapper.PosterMapper;
import cn.wzptsoft.mall.pojo.Poster;
import cn.wzptsoft.mall.pojo.PosterItem;
import cn.wzptsoft.mall.pojo.PublicFeilds;
import cn.wzptsoft.mall.pojo.dto.PosterDto;
import cn.wzptsoft.mall.pojo.vo.PosterItemVo;
import cn.wzptsoft.mall.pojo.vo.PosterVo;
import cn.wzptsoft.mall.service.IPosterItemService;
import cn.wzptsoft.mall.service.IPosterService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IPosterServiceImpl extends ServiceImpl<PosterMapper, Poster> implements IPosterService {

    @Autowired
    private PosterItemMapper posterItemMapper;

    @Autowired
    private IPosterItemService iPosterItemService;



    @Override
    public PosterVo getPoster(String name) {
        //根据姓名查询广告
        Poster poster = lambdaQuery().eq(Poster::getName, name).one();
        if (poster==null){
            throw new RuntimeException("未找到该广告");
        }
        //查询广告的图片信息
        LambdaQueryWrapper<PosterItem> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PosterItem::getPosterId,poster.getId());
        List<PosterItem> posterItems = posterItemMapper.selectList(wrapper);
        //封装到vo的集合
        List<PosterItemVo> list = posterItems.stream().map(PosterItemVo::new).toList();

        return PosterVo.builder()
                .id(poster.getId())
                .description(poster.getDescription())
                .items(list)
                .build();
    }


    @Override
    public void savePoster(PosterDto posterDto) {
        //拷贝poster对象
        Poster poster = BeanUtil.copyProperties(posterDto, Poster.class);
        //根据图片数量拷贝出多个posterItem
        List<PosterItem> posterItemlist = posterDto.getImg().stream().map(PosterItem::new).toList();

        //获取代理类
        IPosterServiceImpl aop = (IPosterServiceImpl) AopContext.currentProxy();

        //调用代理类方法
        poster = (Poster)aop.AutoFullFiledinster(poster);
        //新增poster
        save(poster);
        //给posterItem赋值
        for (PosterItem posterItem : posterItemlist) {
            posterItem = (PosterItem) aop.AutoFullFiledinster(posterItem);
            posterItem.setPosterId(poster.getId());
            posterItem.setSpuId(posterDto.getSpuId());
        }
        iPosterItemService.saveBatch(posterItemlist);

    }

    @Override
    public void updatePoster(PosterDto posterDto) {
        Poster poster = BeanUtil.copyProperties(posterDto, Poster.class);
        //根据图片数量拷贝出多个posterItem
        List<PosterItem> posterItemlist = posterDto.getImg().stream().map(PosterItem::new).toList();

        //获取代理类
        IPosterServiceImpl aop = (IPosterServiceImpl) AopContext.currentProxy();
        //调用代理类方法
        poster = (Poster)aop.AutoFullFiledupdate(poster);
        //更新
        updateById(poster);

        LambdaQueryWrapper<PosterItem> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PosterItem::getPosterId,poster.getId());
        posterItemMapper.delete(wrapper);

        for (PosterItem posterItem : posterItemlist) {
            posterItem = (PosterItem) aop.AutoFullFiledupdate(posterItem);
            posterItem.setPosterId(poster.getId());
            posterItem.setSpuId(posterDto.getSpuId());
        }
        iPosterItemService.saveBatch(posterItemlist);


    }

    @AutoFull(value = OperationType.INSERT)
    public PublicFeilds AutoFullFiledinster(PublicFeilds publicFeilds) {
        System.out.println("-*-/*/*-/-*-*/"+publicFeilds);
        return publicFeilds;
    }

    @AutoFull(value = OperationType.UPDATE)
    public PublicFeilds AutoFullFiledupdate(PublicFeilds publicFeilds) {
        System.out.println("-*-/*/*-/-*-*/"+publicFeilds);
        return publicFeilds;
    }


}
